﻿using System;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using SysComm;
using SysOperationLayer.DBHandle;
using Resources;
using IPMS.Common;

public partial class OptHeadingSection : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        string strCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();

        if (Session["Culture"] != null)
        {
            strCulture = Session["Culture"].ToString();
        }

        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(strCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(strCulture);
        base.InitializeCulture();
    }

    protected void Sort(GridViewRowEventArgs e, GridView gv)
    {
        if ((e.Row != null) && (e.Row.RowType == DataControlRowType.Header))
        {
            foreach (TableCell cell in e.Row.Cells)
            {
                if (cell.HasControls())
                {
                    LinkButton button = (LinkButton)cell.Controls[0];

                    if (button != null)
                    {
                        Image image = new Image();
                        image.ImageUrl = "";

                        if (gv.SortExpression == button.CommandArgument)
                        {
                            if (gv.SortDirection == SortDirection.Ascending)
                            {
                                image.ImageUrl = "images/sortup.gif";
                            }
                            else
                            {
                                image.ImageUrl = "images/sortdown.gif";
                            }
                        }

                        if (image.ImageUrl != "")
                        {
                            cell.Controls.Add(image);
                        }
                    }
                }
            }
        }
    }

    private void showHeadingSections()
    {
        //DataTable dt = ODSHeadingSection.
    }

    private void clearText()
    {
        txtDescription.Text = "";
        txtSection.Text = "";
        hideErrorMsg();
    }

    protected void setTextReadOnly(bool bBool)
    {
        txtSection.ReadOnly = bBool;
        txtDescription.ReadOnly = bBool;
        hideErrorMsg();
    }

    protected void setButtons(string action)
    {
        switch (action)
        {
            case "modify":
                btnModify.Enabled = false;
                btnSave.Enabled = true;
                return;
        }

        btnModify.Enabled = txtSection.Text.Length != 0;
        btnSave.Enabled = false;
    }

    protected void hideErrorMsg()
    {
        lblError.Text = "";
        lblError.Visible = false;
    }

    protected void showErrorMsg(string msg)
    {
        lblError.Text = msg;
        lblError.Visible = true;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!SysUtil.IsAdministratorUsertype())
        {
            base.Response.Redirect("ErrorPage/NotAllowed.aspx");
        }
        else if (!IsPostBack)
        {
            ((Label)Master.FindControl("lblMstTitle")).Text = " | " + ISitemap.Settings + " | " + ISitemap.ProcedureSetup + " | " + ISitemap.HeadingSection;
            setButtons(string.Empty);
            setTextReadOnly(true);
        }
    }

    protected void btnModify_Click(object sender, EventArgs e)
    {
        setButtons("modify");
        setTextReadOnly(false);
        txtSection.ReadOnly = true;
        txtDescription.ReadOnly = false;
        txtDescription.Focus();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string trueUpdateStr = string.Empty;
        string falseUpdateStr = string.Empty;
        string trueInsertStr = string.Empty;
        string falseInsertStr = string.Empty;

        if (Session["Culture"] != null)
        {
            if (Session["Culture"].ToString() == "zh-TW")
            {
                trueUpdateStr = "更新成功!";
                falseUpdateStr = "更新失败!";
                trueInsertStr = "添加成功!";
                falseInsertStr = "添加失敗!";
            }
            else if (Session["Culture"].ToString() == "zh-CN")
            {
                trueUpdateStr = "更新成功!";
                falseUpdateStr = "更新失败!";
                trueInsertStr = "添加成功!";
                falseInsertStr = "添加失败!";
            }
            else
            {
                trueUpdateStr = "Update succeed!";
                falseUpdateStr = "Update failed!";
                trueInsertStr = "Add succeed!";
                falseUpdateStr = "Add failed!";
            }
        }
        try
        {
            ODSHeadingSection.UpdateMethod = "Update";
            ODSHeadingSection.UpdateParameters["Description"].DefaultValue = txtDescription.Text.ToString();
            ODSHeadingSection.UpdateParameters["OwnerGroupID"].DefaultValue = Session["OwnerGroupID"].ToString();
            ODSHeadingSection.UpdateParameters["Section"].DefaultValue = txtSection.Text.ToString();

            LogDB objDB = new LogDB();
            if (ODSHeadingSection.Update() < 0)
            {
                objDB.writeSucLog(SysUtil.m_functiong_procedure, "Update Heading Section Success! Section: " + txtSection.Text.ToString());
                IPMS.Common.Jscript.Alert(trueUpdateStr, Page);
            }
            else
            {
                objDB.writeFaileLog(SysUtil.m_functiong_procedure, "Update Heading Section Failure! Section: " + txtSection.Text.ToString());
                IPMS.Common.Jscript.Alert(falseUpdateStr, Page);
            }
            objDB.CloseDB();

            clearText();
            setTextReadOnly(true);
            setButtons(string.Empty);
        }
        catch (Exception ex)
        {
            lblError.Visible = true;
            showErrorMsg(ex.Message.ToString());
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        clearText();
        setTextReadOnly(true);
        setButtons(string.Empty);
    }

    protected void gvHeadingSection_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtSection.Text = gvHeadingSection.DataKeys[gvHeadingSection.SelectedIndex].Values["Section"].ToString();
        txtDescription.Text = gvHeadingSection.DataKeys[gvHeadingSection.SelectedIndex].Values["Description"].ToString();
        txtSection.Enabled = false;
        txtDescription.Enabled = true;
        txtDescription.Focus();
        setButtons(string.Empty);
    }

    protected void gvHeadingSection_RowCreated(object sender, GridViewRowEventArgs e)
    {
        Sort(e, gvHeadingSection);
    }
}
